package com.yk.framework.gui.ctrl;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import com.yk.framework.gui.core.impl.DefaultF6Format;
import com.yk.framework.gui.ctrl.model.IF6Format;
import com.yk.framework.gui.ctrl.model.IYKF6Selector;
import com.yk.framework.icons.IconReader;

/**
 * @author lifetime
 * 
 */
@SuppressWarnings("serial")
public class YKF6 extends JTextField implements KeyListener, IYKEditor, FocusListener {
	private JButton f6Button;
	private IYKF6Selector selector;
	private Object userObject;
	private IF6Format format;

	public YKF6() {
		this(null);
	}

	public YKF6(IYKF6Selector selector) {
		this.selector = selector;
		this.addKeyListener(this);
		format = new DefaultF6Format();
		f6Button = new JButton(IconReader.getIcon("ykf6_button.gif"));
		f6Button.setBounds(new Rectangle(2, 2, 20, 19));
		f6Button.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent paramMouseEvent) {
				f6Button.setCursor(Cursor.getDefaultCursor());
			}

			public void mouseClicked(MouseEvent e) {
				showSelector();
			}
		});
		this.addFocusListener(this);
		f6Button.addKeyListener(this);
		this.add(f6Button);
	}

	public void setBounds(int x, int y, int width, int height) {
		super.setBounds(x, y, width, height);
		f6Button.setPreferredSize(new Dimension(18, 18));
		f6Button.setBounds(new Rectangle(width - 19, 1, 18, 18));
	}

	public void setText(String text) {
		super.setText(text);
	}

	public Insets getMargin() {
		return new Insets(0, 0, 0, 18);
	}

	public JButton getF6Button() {
		return f6Button;
	}

	public void setF6Button(JButton f6Button) {
		this.f6Button = f6Button;
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				try {
					javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
				} catch (Exception e) {
					e.printStackTrace();
				}
				JFrame jf = new JFrame();
				jf.getContentPane().setLayout(new BorderLayout());
				YKF6 ykf6 = new YKF6();
				JPanel topPanel = new JPanel();
				topPanel.setLayout(null);
				topPanel.setPreferredSize(new Dimension(200, 50));
				ykf6.setBounds(new Rectangle(20, 20, 100, 20));
				jf.setBounds(new Rectangle(0, 0, 400, 500));
				topPanel.add(ykf6);

				JPanel centerPanel = new JPanel();
				centerPanel.setLayout(new BorderLayout());
				JTable table = new JTable(new DefaultTableModel(new Object[][] { {} }, new Object[] { "test" }));
				TableColumn column = table.getColumnModel().getColumn(0);
				YKF6 ykf62 = new YKF6();
				ykf62.setForeground(Color.red);
				column.setCellEditor(new DefaultCellEditor(ykf62));
				table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
				JScrollPane scroll = new JScrollPane(table);
				centerPanel.add(scroll, BorderLayout.CENTER);
				jf.add(topPanel, BorderLayout.NORTH);
				jf.add(centerPanel, BorderLayout.CENTER);
				jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				jf.setVisible(true);
			}
		});
	}

	public IYKF6Selector getSelector() {
		return selector;
	}

	public void setSelector(IYKF6Selector selector) {
		this.selector = selector;
	}

	public Object getUserObject() {
		return userObject;
	}

	public void setUserObject(Object userObject) {
		this.userObject = userObject;
		format.setUserObject(userObject);
		if (this.isFocusOwner()) {
			setText(format.getEditText());
		} else {
			setText(format.getDispalyText());
		}
	}

	public void keyTyped(KeyEvent e) {

	}

	public void keyPressed(KeyEvent e) {
		if (117 == e.getKeyCode()) {
			showSelector();
		}
	}

	protected void showSelector() {
		if (selector != null) {
			selector.showSelector();
			if (!selector.isCancel()) {
				setUserObject(selector.getData());
			}
		}
	}

	public void keyReleased(KeyEvent e) {

	}

	public IF6Format getFormat() {
		return format;
	}

	public void setFormat(IF6Format format) {
		this.format = format;
	}

	public void focusGained(FocusEvent paramFocusEvent) {
		setText(format.getEditText());
	}

	public void focusLost(FocusEvent paramFocusEvent) {
		setText(format.getDispalyText());
	}

}
